package com.example.qixinshangwutong.model.follows;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.view.activity.CompanyActivity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ObjectAdapter extends RecyclerView.Adapter <ObjectAdapter.objectHolder>{
    private List<ObjectModel> companyData;
    private Context context;

    public void setCompanyData(List<ObjectModel> companyData) {
        this.companyData = companyData;
    }

    public ObjectAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @NotNull
    @Override
    public objectHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        return new objectHolder(LayoutInflater.from(context).inflate(R.layout.object_item,null));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull objectHolder holder, int position) {
        if (!companyData.get(position).getImageurl().isEmpty()){
            Glide.with(context).load(companyData.get(position).getImageurl()).into(holder.itemIcon);
        }else {
            holder.itemIcon.setImageResource(R.drawable.shouye);
        }
        holder.itemText.setText(companyData.get(position).getTitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,CompanyActivity.class);
                intent.putExtra("murl",companyData.get(position).getImageurl());
                intent.putExtra("url",companyData.get(position).getUrl());
                intent.putExtra("name",companyData.get(position).getTitle());
                context.startActivity(intent);
            }
        });

    }



    @Override
    public int getItemCount() {
        return null!=companyData?companyData.size():0;
    }
    class objectHolder extends RecyclerView.ViewHolder{
        ImageView itemIcon;
        TextView itemText;

        public objectHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            itemIcon=itemView.findViewById(R.id.object_item_icon);
            itemText=itemView.findViewById(R.id.object_item_text);
        }
    }
}
